﻿
using System.Collections.Generic;
using System.Data;
using System;
using System.Text;
using System.Data.SqlClient;
using SXT.DB;
namespace ZHDJ.MobileInterface
{
    public partial class AddScore : AbstractBasePage
    {
        protected override OutParam BusinessProcess<InParam, OutParam>(InParam inParam)
        {
            // 业务逻辑返回的响应对象  
            AddScoreOutParam result = new AddScoreOutParam();
            AddScoreInParam param = new AddScoreInParam();
            string logMsg = "";
            bool isFlag = true;
                 
            try
            {
                param = inParam as AddScoreInParam;

                string category = "";
                string defalutDesc = param.scoreDesc;

                if (string.IsNullOrEmpty(param.user_id) || string.IsNullOrEmpty(param.foreignID) || string.IsNullOrEmpty(param.scoreCode))
                {
                    result.code = "0";
                    result.desc = "用户ID,外键ID,积分编码类型不能为空";

                    isFlag = false;
                } 

                if (isFlag)
                {
                    //用户是否存在
                    isFlag = InterfaceHelper.CheckLoginIDCountEx(param.user_id);

                    if (!isFlag)
                    {
                        result.code = "0";
                        result.desc = "该用户不存在";
                    }
                }

                if (isFlag)
                {
                    // 获取积分编码类型 
                    DataSet ds = InterfaceHelper.GetScoreCode(param.scoreCode);

                    if (ds == null || ds.Tables[0].Rows.Count == 0)
                    {
                        result.code = "0";
                        result.desc = "积分编码类型不存在";

                        isFlag = false;
                    }
                    else if (ds.Tables[0].Rows.Count > 1)
                    {
                        result.code = "0";
                        result.desc = "积分编码类型有重复，请联系党建平台管理员";

                        isFlag = false;
                    }
                    else
                    {
                        category = Convert.ToString(ds.Tables[0].Rows[0]["Category"]);

                        if (string.IsNullOrEmpty(defalutDesc))
                        { defalutDesc = Convert.ToString(ds.Tables[0].Rows[0]["DefalutDesc"]); }
                    }
                }

                if (isFlag)
                {
                    if (string.IsNullOrEmpty(param.ColumnEx))
                    { param.ColumnEx = ""; }

                    result.desc = InterfaceHelper.ScoreManage(category, "0", param.foreignID, param.user_id, defalutDesc, param.ColumnEx);
                    
                    
                    result.code = (result.desc == "操作成功" ? "1" : "0");
                    if (result.code == "1")
                    {
                        AddScoreRecord(param, category);
                    }
                }
                 
                logMsg = result.desc;
            }
            catch (Exception ex)
            {
                result.code = "0";
                result.desc = "操作不成功！"; 
                logMsg = ex.Message + ":" + ex.StackTrace;
            }

            CommonUtils.InsertMobileInterfaceLog(SystemCode +"-AddScore", SerializeHelper.JsonSerializer<AddScoreInParam>(param), logMsg); 
            return result as OutParam;
        }

        public void AddScoreRecord(AddScoreInParam param, string category)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    StringBuilder sbSql = new StringBuilder();
                    sbSql.Append(@"insert tblPartyMSAddScoreRecord (RecordID,ObjectID,ObjectType,scoreCode,Category,ForeignID,[Description],CreateTime,ColumnEx)
                            values ( replace(newid(), '-',''),@ObjectID,'Member',@scoreCode,@Category,@ForeignID,@Description,getdate(),@ColumnEx)");
                    cmd.CommandText = sbSql.ToString();
                    cmd.Parameters.Add("@ObjectID", SqlDbType.NVarChar).Value = param.user_id;
                    cmd.Parameters.Add("@Category", SqlDbType.NVarChar).Value = category;
                    cmd.Parameters.Add("@scoreCode", SqlDbType.NVarChar).Value = param.scoreCode;
                    cmd.Parameters.Add("@ForeignID", SqlDbType.NVarChar).Value = param.foreignID;
                    cmd.Parameters.Add("@Description", SqlDbType.NVarChar).Value = param.scoreDesc;
                    cmd.Parameters.Add("@ColumnEx", SqlDbType.NVarChar).Value = param.ColumnEx;
                    DBManager.DBSettings["SXT.DefaultDBEx"].ExecuteNonQuery(cmd, false);
                }
            }
            catch (Exception)
            {
                
            }
        }

        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (string.Compare(Request.QueryString["Safe"], "False", true) == 0)
                ProcessRequest<AddScoreInParam, AddScoreOutParam>();
            else
                SafeProcessRequest<AddScoreInParam, AddScoreOutParam>();
        }
    }
     
    #region 请求参数
    public class AddScoreInParam
    {
        /// <summary>
        ///	用户ID 必须
        /// </summary>
        public string user_id
        { get; set; }  

        /// <summary>
        ///	积分编码类型 必须
        /// </summary>
        public string scoreCode
        { get; set; }

        /// <summary>
        ///	外键Id  必须
        /// </summary>
        public string foreignID
        { get; set; }   

        /// <summary>
        ///	积分描述 可为空
        /// </summary>
        public string scoreDesc
        { get; set; }

        /// <summary>
        ///	扩展字段 可为空留待以后备用
        /// </summary>
        public string ColumnEx
        { get; set; }  
    }
    #endregion

    #region 结果参数
    public class AddScoreOutParam
    {
        public AddScoreOutParam()
        {
            code = "1";
            desc = "操作成功"; 
        }

        /// <summary>
        /// 返回结果 具体见接口返回对照码 1	成功 0	失败 2	请求非法 加密结果和传入的MD5字符串进行比较，如果相同则执行接口操作，否则返回1001错误响应码
        /// </summary>
        public string code
        { get; set; }

        /// <summary>
        /// 结果描述 成功和失败都需要
        /// </summary>
        public string desc
        { get; set; } 
    } 
    #endregion 
}